<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>头像上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            margin-top: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="file"] {
            margin-bottom: 10px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        #preview {
            max-width: 200px;
            max-height: 200px;
            margin-top: 10px;
            display: none;
        }
        #result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>头像上传测试</h2>
        <div class="form-group">
            <label for="avatar">选择头像文件：</label>
            <input type="file" id="avatar" accept="image/*">
            <img id="preview" alt="预览图">
        </div>
        <div class="form-group">
            <button onclick="uploadAvatar()">上传头像</button>
        </div>
        <div id="result"></div>
    </div>

    <script>
        // 显示图片预览
        document.getElementById('avatar').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.getElementById('preview');
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                }
                reader.readAsDataURL(file);
            }
        });

        // 上传头像
        function uploadAvatar() {
            const fileInput = document.getElementById('avatar');
            const file = fileInput.files[0];
            const resultDiv = document.getElementById('result');

            if (!file) {
                resultDiv.className = 'error';
                resultDiv.textContent = '请选择要上传的头像文件';
                return;
            }

            // 检查文件大小（2MB）
            if (file.size > 2 * 1024 * 1024) {
                resultDiv.className = 'error';
                resultDiv.textContent = '文件大小不能超过2MB';
                return;
            }

            // 检查文件类型
            if (!file.type.startsWith('image/')) {
                resultDiv.className = 'error';
                resultDiv.textContent = '只支持上传图片文件';
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            // 使用固定的token
            const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzY2hvb2xib29rc2hvcCIsImlhdCI6MTc0NDk0NzM0NCwiZXhwIjoxNzQ1MDMzNzQ0LCJ1c2VySWQiOiI1IiwidXNlcm5hbWUiOiJ6aGFuZ3NhbiIsInJvbGUiOiJTVFVERU5UIn0.LMKw2aCBvjDTvDtptWAbVI8c7qEfhPdK4h_kN2mfkxs';

            fetch('http://localhost:8080/api/profile/uploadAvatar', {
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + token
                },
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    resultDiv.className = 'success';
                    resultDiv.textContent = '头像上传成功';
                } else {
                    resultDiv.className = 'error';
                    resultDiv.textContent = '上传失败：' + data.message;
                }
            })
            .catch(error => {
                resultDiv.className = 'error';
                resultDiv.textContent = '上传出错：' + error.message;
            });
        }
    </script>
</body>
</html> 